/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.ball.utils.handlers
{
	import com.puny.utils.easing.BothEndsTrigonometricEasing;
	

	public class VerticalMovingHandler extends MovingHandler implements IMovingHandler
	{
		private var _upward:BothEndsTrigonometricEasing;
		private var _downward:BothEndsTrigonometricEasing;

		public function VerticalMovingHandler()
		{
		}

		public function motionDefinition(x:Number, y:Number):void
		{
			org.x = x;
			org.y = y;
			_upward		= new BothEndsTrigonometricEasing(y,			y + amplitude,	cycle / 2, 1);
			_downward	= new BothEndsTrigonometricEasing(y + amplitude,	y,			cycle / 2, 1);
			easing = _downward;
		}

		protected function get amplitude():Number {
			return 80;
		}
		
		protected function get cycle():Number {
			return 24;
		}
		
		public function initialize():void
		{
			easing = easing == _upward ? _downward : _upward;
			easing.initialize();
			v.y = easing.forward() - org.y;			
		}
		
		public function nextPosition():Boolean
		{
			org.x += v.x;
			org.y += v.y;
			var ret:Boolean = easing.isMoving();
			if (ret) {
				v.y = easing.forward() - org.y;
			} else {
				v.y = 0;
			}
			return ret;
		}

		public function resume(dir:String, pos:int):void {
			easing = dir == "upward" ? _upward : _downward;
			easing.pos = pos;
		}
		
		public function debug():String {
			var string:String = "\t\t\tctrl.jump(";
			string += easing == _upward ? "\"upward\"" : "\"downward\"";
			string += ", ";
			string += easing.pos;
			string += ");\r\n";
			return string;
		}
		
	}
}